package com.woniuxy.controller;

import com.woniuxy.dao.CarMapper;
import com.woniuxy.pojo.Car;
import com.woniuxy.pojo.CarPic;
import com.woniuxy.pojo.CurrentUnit;
import com.woniuxy.pojo.Dictionary;
import com.woniuxy.service.CarService;
import com.woniuxy.util.DataGridView;
import com.woniuxy.util.ResultDto;
import com.woniuxy.vo.CarVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CarController extends BaseController{

    Logger logger= LoggerFactory.getLogger(this.getClass());
    @Autowired
    private CarService carService;
    @Autowired
    private CarMapper carMapper;
    @GetMapping("/cars")
    @ResponseBody
    public DataGridView selectCarsByPage(CarVo carVo){
        return carService.getCarListByPage(carVo);
    }
    @RequestMapping(value = "/toCarManager.do")
    public String toCarManager(){
        return "car/carList";
    }
    @RequestMapping(value = "/toCarAddPage.do")
    public String toCarAddPage(){
        return "car/carAdd";
    }
    @RequestMapping(value = "/toCarEditPage.do")
    public String toCarEditPage(){
        return "car/carEdit";
    }
    @RequestMapping(value = "/toCarPicPage.do")
    public String toCarPicPage(){
        return "car/carPic";
    }
    @RequestMapping(value = "/toCarStatePage.do")
    public String toCarStatePage(){
        return "car/carstate";
    }

    @PostMapping(value = "/cars.do")
    @ResponseBody
    public ResultDto addCar(CarVo carVo){
        ResultDto dto=new ResultDto();
        Car carResult=this.carService.findCarByCarNo(carVo.getCar_no());
        if(carResult!=null){
            return ResultDto.CAR_EXITS;
        }else{
            boolean flag=this.carService.addCar(carVo);
            if(flag){
                return ResultDto.CAR_ADD_SUCCESS;
            }else{
                return ResultDto.CAR_ADD_FAILURE;
            }
        }
    }


    @PostMapping(value = "/carPic.do")
    @ResponseBody
    public ResultDto carPicManage(String cid,String des,String img_path) {
        ResultDto dto = new ResultDto();
        int id=Integer.parseInt(cid);
        String[] dess=des.split(",");
        String[] imgs=img_path.split(",");
        System.out.println(cid+'?'+des+'?'+img_path);
        for(int i=0;i<3;i++){
            CarPic carPics=new CarPic();
            carPics.setCid(id);
            carPics.setDes(dess[i]);
            carPics.setImg_path(imgs[i]);
//            if( this.carService.findCarPic(carPics)!=null){
//                    this.carService.updateCarPic(carPics);
//                }else{
//                    this.carService.addCarPic(carPics);
//                }
            if("45".equals(dess[i])){
                if( this.carService.findCarPicBackByCid(id)!=null){
                    this.carService.updateCarPic(carPics);
                }else{
                    this.carService.addCarPic(carPics);
                }
            }else if("Pos".equals(dess[i])){
                if( this.carService.findCarPicPosByCid(id)!=null){
                    this.carService.updateCarPic(carPics);
                }else{
                    this.carService.addCarPic(carPics);
                }
            }else if("Back".equals(dess[i])){
                if( this.carService.findCarPicBackByCid(id)!=null){
                    this.carService.updateCarPic(carPics);
                }else{
                    this.carService.addCarPic(carPics);
                }
            }
        }
        return ResultDto.UPDATE_CAR_PIC_SUCCESS;
    }
    @PostMapping("/editcars.do")
    @ResponseBody
    public ResultDto upadteCar(Car car){
        try{
            boolean flag=carService.update(car);
            if(flag){
                return ResultDto.UPDATE_CAR_SUCCESS;
            }else{
                return ResultDto.UPDATE_CAR_FAILURE;
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResultDto.UPDATE_CAR_FAILURE;
        }
    }
    @GetMapping(value = "/car/{id}")
    @ResponseBody
    public ResultDto deleteCar(@PathVariable("id") int id){
        try{
            this.carService.removeCar(id);
            return ResultDto.DELETE_CAR_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultDto.DELETE_CAR_FAILURE;
        }
    }

    @GetMapping(value = "/car/carPic/{cid}")
    @ResponseBody
    public List<CarPic> findCarPicByCid(@PathVariable("cid") int cid){
            return carService.findCarPicByCid(cid);
    }

    @RequestMapping(value = "/car/deleteBatch")
    @ResponseBody
    public ResultDto deleteBathCar(@RequestParam("ids[]") Integer[] ids){
        try{
            this.carService.removeBathCar(ids);
            return ResultDto.DELETE_CAR_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultDto.DELETE_CAR_FAILURE;
        }
    }

    @GetMapping(value = "/car/carbrand")
    @ResponseBody
    public List<Dictionary> carbrand(){
        return carMapper.selectAllCarBrands();
    }
    @GetMapping(value = "/car/carmodel/{id}")
    @ResponseBody
    public List<Dictionary> carmodel(@PathVariable("id") Integer id){
        return carMapper.selectAllCarModels(id);
    }
    @GetMapping(value = "/car/carcolor")
    @ResponseBody
    public List<Dictionary> carcolor(){
        return carMapper.selectAllCarColors();
    }
    @GetMapping(value = "/car/supid")
    @ResponseBody
    public List<CurrentUnit> supid(){
        return carMapper.selectAllSupIds();
    }
    @GetMapping(value = "/car/deptid")
    @ResponseBody
    public List<Dictionary> deptid(){
        return carMapper.selectAllDeptIds();
    }
    @GetMapping(value = "/car/carstate")
    @ResponseBody
    public List<Dictionary> carstate(){
        return carMapper.selectAllCarStates();
    }
}
